﻿/************************************************************************

 This file is part of EscapeFromPrisonPlanet.

 EscapeFromPrisonPlanet is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 EscapeFromPrisonPlanet is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with Foobar.  If not, see <http://www.gnu.org/licenses/>.

************************************************************************/

// All code (c)2012 Scott Bevin. all rights reserved

using LudumDare25Project.Objects;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace LudumDare25Project
{
    internal class HUD
    {
        private Texture2D mainHudTex;
        private Texture2D healthMeter;
        private Texture2D ammoMeter;
        private Texture2D missileTex;

        private SpriteFont hintFont;

        private Spaceship player;

        private float MsgMaxTime = 7.5f;

        private Vector2[] MissilePositions = new Vector2[Spaceship.MaxMissiles];

        private class HintMessage
        {
            public string Text = "";
            public float Timer = 0.0f;
        }

        public HUD(Spaceship plr, ContentManager content)
        {
            mainHudTex = content.Load<Texture2D>("Img/HUD/HudMain");
            healthMeter = content.Load<Texture2D>("Img/HUD/HealthMeter");
            ammoMeter = content.Load<Texture2D>("Img/HUD/AmmoMeter");
            missileTex = content.Load<Texture2D>("Img/Missile");

            hintFont = content.Load<SpriteFont>("Fonts/HUD");

            player = plr;

            MissilePositions[0] = new Vector2(260.0f, 560.0f);
            MissilePositions[1] = new Vector2(285.0f, 560.0f);
            MissilePositions[2] = new Vector2(310.0f, 560.0f);
            MissilePositions[3] = new Vector2(690.0f, 560.0f);
            MissilePositions[4] = new Vector2(715.0f, 560.0f);
            MissilePositions[5] = new Vector2(740.0f, 560.0f);
        }

        public void Render(SpriteBatch sprites, float distance)
        {
            sprites.Begin();

            sprites.Draw(healthMeter, new Vector2(10.0f, 510.0f),
                new Rectangle(0, 0, (int)((float)healthMeter.Width * (player.HullHealth * 0.01f)), healthMeter.Height), Color.Lerp(Color.Green, Color.Red, 1.0f - (player.HullHealth * 0.01f)));

            int wRem = (int)((float)ammoMeter.Width * (player.Shield * 0.01f));
            sprites.Draw(ammoMeter, new Vector2(800.0f + ammoMeter.Width - wRem, 510.0f),
                new Rectangle(ammoMeter.Width - wRem, 0, wRem, ammoMeter.Height), Color.Lerp(Color.Green, Color.Red, 1.0f - player.Shield * 0.01f));

            sprites.Draw(mainHudTex, new Rectangle(0, 500, 1000, 100), Color.White);

            for (int i = 0; i < player.Missiles; ++i)
            {
                sprites.Draw(missileTex, MissilePositions[i], null, Color.White, -MathHelper.PiOver2,
                    new Vector2(missileTex.Width, missileTex.Height) * 0.5f, 1.0f, SpriteEffects.None, 0.0f);
            }

            string hintText = distance.ToString();

            sprites.DrawString(
                hintFont,
                hintText,
                new Vector2(500.0f, 575.0f),
                Color.Green,
                0.0f,
                hintFont.MeasureString(hintText) * 0.5f,
                1.0f,
                SpriteEffects.None, 0.0f);

            sprites.End();
        }
    }
}